package com.guli.file.config;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadFileWriter;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@Component
public class FastDfSClient {
    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @Autowired
    private FdfsWebServer fdfsWebServer;

    @Autowired
    private ThumbImageConfig thumbImageConfig;
    /**
     * 通过HTTP上传文件
     * @param file
     * @return
     */
    public String upload(MultipartFile file){
        try {
            StorePath storePath = fastFileStorageClient.uploadFile(
                    file.getInputStream(),
                    file.getSize(),
                    FilenameUtils.getExtension(file.getOriginalFilename()),
                    null);

            return getResAccessUrl(storePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }
    // 封装图片完整URL地址
    private String getResAccessUrl(StorePath storePath){
        //fdfsWebServer.getWebServerUrl()=http://47.94.97.131/
        //getFullPath = group1/M00/12/ed/文件名
        String fileUrl=fdfsWebServer.getWebServerUrl()+storePath.getFullPath();
        return fileUrl;
    }
    // 封装图片完整URL地址 缩略图
    private String getThumbImageResAccessUrl(StorePath storePath){
        //fdfsWebServer.getWebServerUrl()=http://47.94.97.131/
        //getFullPath = group1/M00/12/ed/文件名
        String slavePath = thumbImageConfig.getThumbImagePath(storePath.getPath());
        String fileUrl  = fdfsWebServer.getWebServerUrl()+slavePath;
        return fileUrl;

    }

    //上传缩略图
    public String uploadThumbImage(MultipartFile file){
        try {
            StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(
                    file.getInputStream(),
                    file.getSize(),
                    FilenameUtils.getExtension(file.getOriginalFilename()),
                    null);
            return getThumbImageResAccessUrl(storePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //本地上传文件
    public  String upload(File file) throws IOException {

            FileInputStream fileInputStream = new FileInputStream(file);
            StorePath storePath = fastFileStorageClient.uploadFile(
                    fileInputStream,
                    file.length(),
                    FilenameUtils.getExtension(file.getAbsolutePath()),
                    null);
            return  storePath.getFullPath();
    }
        //下载文件到本地
    public void download(String fileUrl,String localFileName){
        StorePath storePath = StorePath.parseFromUrl(fileUrl);
        fastFileStorageClient.downloadFile(
                storePath.getGroup(),
                storePath.getPath(),
                new DownloadFileWriter(localFileName));

    }



}
